package com.example.my_listview;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.ActionMode;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private List<name> data = new ArrayList<>();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        View etWeight = findViewById(R.id.lv_list);
        registerForContextMenu(etWeight);

        for (int i = 0; i < 10; i++) {
            name na =new name();
            na.setName("姓名" + i);
            data.add(na);

        }

        ListView listView = findViewById(R.id.lv_list);
        listView.setAdapter(new MyAdapter(data,this));


    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        if (v.getId() == R.id.lv_list){
            //加载菜单XML资源
            MenuInflater inflater = getMenuInflater();
            inflater.inflate(R.menu.popupmenu,menu);
            //创建动态菜单项
            menu.add(Menu.NONE,Menu.FIRST,0,"清除");
        }
    }

    @Override
    public boolean onContextItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()){
            case R.id.item_del:
                Toast.makeText(MainActivity.this, item.getTitle(), Toast.LENGTH_SHORT).show();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }
    //上下文操作模式对象
    private ActionMode actionMode;

    ActionMode.Callback actionModeCallback = new ActionMode.Callback(){
        //在启动上下文操作模式startActionMode时调用
        //在次配置上下问菜单的资源
        @Override
        public boolean onCreateActionMode(ActionMode mode, Menu menu) {
            mode.getMenuInflater().inflate(R.menu.popupmenu,menu);
            return true;
        }

        //在创建方法后进行调用
        @Override
        public boolean onPrepareActionMode(ActionMode actionMode, Menu menu) {
            return false;
        }

        //菜单项被单击，类似onContextItemSelected()方法
        @Override
        public boolean onActionItemClicked(ActionMode actionMode, MenuItem menuItem) {
            switch (menuItem.getItemId()){
                case R.id.item_del:
                    Toast.makeText(MainActivity.this,"删除",Toast.LENGTH_SHORT).show();
                    break;
                default:
                    break;
            }
            return true;
        }
        //上下文操作模式结束时被调用
        @Override
        public void onDestroyActionMode(ActionMode actionMode) {
            actionMode = null;
        }
    };




}